package com.complaint.rest.rc;


import com.complaint.rest.model.Complaint;
import com.complaint.rest.model.User;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;

import javax.ws.rs.core.MediaType;
import java.util.List;

/**
 * Created by Alex
 */
public class RestClient {

    //    private static final String REST_SERVICE_URL = "http://localhost:8080/rest/";
    private static final String REST_SERVICE_URL = "http://complaintserviceserver.appspot.com/rest/";
    private static final String USER = "user";
    private static final String COMPLAINT = "complaint";

    private final WebResource resource;
    private static RestClient restClient = null;

    private RestClient() {
        resource = Client.create().resource(REST_SERVICE_URL);
    }

    public static RestClient get() {
        if (restClient == null) {
            restClient = new RestClient();
        }
        return restClient;
    }

    public User addUser(User user) {
        User persistedUser = null;
        ClientResponse response = resource.path(USER).type(MediaType.APPLICATION_XML).post(ClientResponse.class, user);
        if (response.getStatus() == 200 && response.hasEntity()) {
            persistedUser = response.getEntity(User.class);
        }
        return persistedUser;
    }

    public void removeUser(User user) {
        resource.path(USER + "/" + user.getId()).type(MediaType.APPLICATION_XML).delete();
    }

    public User updateUser(User user) {
        User persistedUser = null;
        ClientResponse response = resource.path(USER).type(MediaType.APPLICATION_XML).post(ClientResponse.class, user);
        if (response.getStatus() == 200 && response.hasEntity()) {
            persistedUser = response.getEntity(User.class);
        }
        return persistedUser;
    }

    public User getUser(String id) {
        User persistedUser = null;
        ClientResponse response = resource.path(USER + "/" + id).type(MediaType.APPLICATION_XML).get(ClientResponse.class);
        if (response.getStatus() == 200 && response.hasEntity()) {
            persistedUser = response.getEntity(User.class);
        }
        return persistedUser;
    }

    public User getUserByEmail(String email) {
        User persistedUser = null;
        ClientResponse response = resource.path(USER + "/email/" + email).type(MediaType.APPLICATION_XML).get(ClientResponse.class);
        if (response.getStatus() == 200 && response.hasEntity()) {
            persistedUser = response.getEntity(User.class);
        }
        return persistedUser;
    }

    public Complaint addComplaint(Complaint complaint) {
        Complaint persistedComplaint = null;
        ClientResponse response = resource.path(COMPLAINT).type(MediaType.APPLICATION_XML).post(ClientResponse.class, complaint);
        if (response.getStatus() == 200 && response.hasEntity()) {
            persistedComplaint = response.getEntity(Complaint.class);
        }
        return persistedComplaint;
    }

    public void removeComplaint(Complaint complaint) {
        resource.path(COMPLAINT + "/" + complaint.getId()).type(MediaType.APPLICATION_XML).delete();
    }

    public Complaint updateComplaint(Complaint complaint) {
        Complaint persistedComplaint = null;
        ClientResponse response = resource.path(COMPLAINT).type(MediaType.APPLICATION_XML).post(ClientResponse.class, complaint);
        if (response.getStatus() == 200 && response.hasEntity()) {
            persistedComplaint = response.getEntity(Complaint.class);
        }
        return persistedComplaint;
    }

    public Complaint getComplaint(String id) {
        Complaint persistedComplaint = null;
        ClientResponse response = resource.path(COMPLAINT + "/" + id).type(MediaType.APPLICATION_XML).get(ClientResponse.class);
        if (response.getStatus() == 200 && response.hasEntity()) {
            persistedComplaint = response.getEntity(Complaint.class);
        }
        return persistedComplaint;
    }

    public List<Complaint> getComplaintsByUserId(String id) {
        GenericType<List<Complaint>> complaintType = new GenericType<List<Complaint>>() {
        };
        List<Complaint> complaints = null;
        ClientResponse response = resource.path(COMPLAINT + "/" + USER + "/" + id).type(MediaType.APPLICATION_XML).get(ClientResponse.class);
        if (response.getStatus() == 200 && response.hasEntity()) {
            complaints = response.getEntity(complaintType);
        }
        return complaints;
    }

    public List<Complaint> listComplaints() {
        GenericType<List<Complaint>> complaintType = new GenericType<List<Complaint>>() {
        };
        List<Complaint> complaints = null;
        ClientResponse response = resource.path(COMPLAINT).type(MediaType.APPLICATION_XML).get(ClientResponse.class);
        if (response.getStatus() == 200 && response.hasEntity()) {
            complaints = response.getEntity(complaintType);
        }
        return complaints;
    }
}
